package com.example.demo.serialize;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.TestEntity;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

//@JsonComponent
public class PublicSerialize {

    /**
     * 序列化
     */
    public static class Serializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            System.out.println("序列化serialize1....");
            String json = JSONObject.toJSONString(o);
            jsonGenerator.writeString(json);
        }
    }

    /**
     * 反序列化
     */
    public static class Deserializer extends JsonDeserializer<Object> implements ContextualDeserializer {
        private Class<?> targetClass;

        public Deserializer() {
            System.out.println("Deserializer......");
        }

        public Deserializer(Class<?> targetClass) {
            this.targetClass = targetClass;
        }

        @SneakyThrows
        @Override
        public Object deserialize(JsonParser jsonParser, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            System.out.println("反序列化deserialize...");
            String treeNode = jsonParser.getCodec().readTree(jsonParser).toString();
            JSONObject jsonObject = JSONObject.parseObject(treeNode);
            System.out.println("jsonObject:"+jsonObject);
            Object model = targetClass.newInstance();
            //Now I have an instance of the annotated class I can populate the fields via reflection
            Field[] fields = model.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String type = field.getGenericType().toString(); // 获取属性的类型
                String name = field.getName();
                System.out.println("field.getName()："+field.getName());
                System.out.println("jsonObject.get(field.getName())："+jsonObject.get(field.getName()));
                if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class "，后面跟类名
                    field.set(name,"999999");
                }
                if (type.equals("class java.lang.Integer")) {

                }
                if (type.equals("class java.lang.Boolean")) {

                }
                if (type.equals("class java.util.Date")) {

                }
                field.set(field.getName(),jsonObject.get(field.getName()));
            }

            //System.out.println("clazz:"+clazz.toString());
            return model;
        }

        @Override
        public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
                                                    BeanProperty property) throws JsonMappingException {
            System.out.println("createContextual...");
            //gets the class type of the annotated class
            targetClass = ctxt.getContextualType().getRawClass();
            //this new JsonApiDeserializer will be cached
            return new Deserializer(targetClass);
        }

       /* @Override
        public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            System.out.println("反序列化deserialize1......");
            String treeNode = jsonParser.getCodec().readTree(jsonParser).toString();
            System.out.println("treeNode:"+treeNode);
            TestEntity testEntity = JSONObject.toJavaObject(JSON.parseObject(treeNode), TestEntity.class);
            JSONObject jsonObject = JSONObject.parseObject(treeNode);
            testEntity.setCode("500");
            return testEntity;
        }*/
    }

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //Field[] fields = TestEntity.class.getDeclaredFields();
        TestEntity testEntity = new TestEntity();
        Field filed = testEntity.getClass().getDeclaredField("name");
        filed.setAccessible(true);
        filed.set("name","8989");
        System.out.println(TestEntity.class);
        /*for (Field field : fields) {
            System.out.println("field:"+field.getName());

        }*/
    }

}
